package utils.utilcode.blankj.com.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017-12-04.
 */

public class Canvas extends View {
    private Paint mPaint;
    private float t;
    private Handler m = new Handler(Looper.getMainLooper());
    private Rect mLeft_top_Rect, mLeft_bottom_Rect, mRight_top_Rect, mRight_bottom_Rect;
    private RectF mRectF;
    private GestureDetector gestureDetector;
    private Context context;
    private float mAnimatedValue;

    public Canvas(Context context) {
        this(context, null);
    }

    public Canvas(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Canvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        gestureDetector = new GestureDetector(context, new MyOnGestureListener());
        mRectF = new RectF(250, 250, 450, 450);
        mPaint = new Paint();
        mLeft_top_Rect = new Rect(250, 250, 350, 350);
        mLeft_bottom_Rect = new Rect(250, 350, 350, 450);
        mRight_top_Rect = new Rect(350, 250, 450, 350);
        mRight_bottom_Rect = new Rect(350, 350, 450, 450);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i <= 10; i++) {
//                    t += 10;
//                    SystemClock.sleep(500);
//                    m.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            invalidate();
//
//                        }
//                    });
//                }
//                将需要判断的区域区域用Rect包裹起来，rect.contains()
//            }
//        }).start();
//      m.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                for (int i=0;i<=10;i++) {
//                    t += 10;
//                    invalidate();
//                }
//
//            }
//        }, 500);
    }

    @Override
    protected void onDraw(android.graphics.Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLUE);
        mPaint.setColor(Color.BLACK);
        canvas.drawRect(new RectF(20, 20, 100, 100), mPaint);
        canvas.save();
        //画布平移，正向右，负向左
        //旋转：正为顺时针
     canvas.rotate(30);
        mPaint.setColor(Color.WHITE);
        canvas.drawRect(mRectF, mPaint);
//        canvas.restore();
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(new RectF(20, 20, 100, 100), mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }

    class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {

            int posiionX = (int) e.getX();

            int posiionY = (int) e.getY();
            Log.e("MyOnGestureListener", "posiionX " + posiionX + " posiionY " + posiionY);
            Log.e("MyOnGestureListener", "e.getRawX " + e.getRawX() + " e.getRawY " + e.getRawY());

            //左上
            if (isLeftTop(posiionX, posiionY)) {
                Toast.makeText(context, "点击了左上角", Toast.LENGTH_SHORT).show();
            }

            if (isLeftBottom(posiionX, posiionY)) {
                Toast.makeText(context, "点击了左下角", Toast.LENGTH_SHORT).show();
            }
            if (isRightTop(posiionX, posiionY)) {
                Toast.makeText(context, "点击了右上角", Toast.LENGTH_SHORT).show();
            }
            if (isRightBottom(posiionX, posiionY)) {
                Toast.makeText(context, "点击了右下角", Toast.LENGTH_SHORT).show();
            }


            return super.onSingleTapConfirmed(e);
        }
    }

    private boolean isRightBottom(int posiionX, int posiionY) {
        return mRight_bottom_Rect.contains(posiionX, posiionY);
    }

    private boolean isRightTop(int posiionX, int posiionY) {
        return mRight_top_Rect.contains(posiionX, posiionY);
    }

    private boolean isLeftBottom(int posiionX, int posiionY) {
        return mLeft_bottom_Rect.contains(posiionX, posiionY);
    }

    private boolean isLeftTop(int posiionX, int posiionY) {

        return mLeft_top_Rect.contains(posiionX, posiionY);
    }


}
